package com.arpa.oms.service.impl;

import cn.hutool.core.util.IdUtil;
import com.arpa.ntocc.common.common.util.CommonUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.oms.domain.dto.OmsAllocationItemDTO;
import com.arpa.oms.domain.entity.OmsAllocationItem;
import com.arpa.oms.domain.vo.OmsAllocationItemVO;
import com.arpa.oms.mapper.OmsAllocationItemMapper;
import com.arpa.oms.service.IOmsAllocationItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * OMS调拨单明细 服务实现类
 * </p>
 *
 * @author LEO
 * @since 2020-11-17
 */
@Service
public class OmsAllocationItemServiceImpl extends ServiceImpl<OmsAllocationItemMapper, OmsAllocationItem> implements IOmsAllocationItemService {

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public boolean save(OmsAllocationItem entity) {
        if(StringUtils.isBlank(entity.getCode())){
            entity.setCode(IdUtil.simpleUUID());
        }
        if(StringUtils.isBlank(entity.getCreatedBy())){
            entity.setCreatedBy(UserUtil.getCode());
        }
        return super.save(entity);
    }

    /**
     * 根据code更新实体
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public int updateByCode(OmsAllocationItem entity) {
        if(StringUtils.isBlank(entity.getModifiedBy())){
            entity.setModifiedBy(UserUtil.getCode());
        }
        return baseMapper.update(entity,new QueryWrapper<OmsAllocationItem>().lambda().eq(OmsAllocationItem::getCode,entity.getCode()));
    }

    /**
     * 查询列表
     * @param omsAllocationItemDTO
     * @return
     */
    @Override
    public List<OmsAllocationItemVO> queryList(OmsAllocationItemDTO omsAllocationItemDTO){
        //排序字段名需要驼峰转数据库下划线类型字段名
        if(StringUtils.isNotEmpty(omsAllocationItemDTO.getSortField())){
            omsAllocationItemDTO.setSortField(CommonUtil.camel2Underline(omsAllocationItemDTO.getSortField()));
        }
        List<OmsAllocationItemVO> omsAllocationItemVOList = baseMapper.queryList(omsAllocationItemDTO);
        return omsAllocationItemVOList;
    }

    /**
     * 查询合计,包含总数
     * @param omsAllocationItemDTO
     * @return
     */
    @Override
    public OmsAllocationItemVO queryListSum(OmsAllocationItemDTO omsAllocationItemDTO){
        OmsAllocationItemVO omsAllocationItemVO = baseMapper.queryListSum(omsAllocationItemDTO);
        return omsAllocationItemVO;
    }

}
